//
//  XHTrafficUseViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHTrafficUseViewController.h"
#import "XHUseTrafficViewController.h"

#import "XHTrafficSelectCell.h"

#import "Services.h"

@interface XHTrafficUseViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSArray *m_traffic;

@end

@implementation XHTrafficUseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHTrafficSelectCell" bundle:nil] forCellReuseIdentifier:XHTrafficSelectCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getSelectFinancialBenefitsTrafficWithOrderSn];
}

- (void)getSelectFinancialBenefitsTrafficWithOrderSn
{
    [ServiceManager getSelectFinancialBenefitsTrafficWithOrderSn:self.orderSn success:^(NSDictionary *data) {
        self.m_traffic = (NSArray*)data;
        [self.ui_tableView reloadData];
    } failure:^(NSDictionary *data) {
        
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = 1;
    
    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=self.m_traffic.count;
    
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    cell = [self trafficSelectCell:indexPath];
    
    return cell;
}

- (XHTrafficSelectCell*)trafficSelectCell:(NSIndexPath*)indexPath
{
    XHTrafficSelectCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHTrafficSelectCellIdentifier];
    NSDictionary * dic = self.m_traffic[indexPath.row];
    cell.ui_numberLabel.text = [NSString stringWithFormat:@"可用数量:(%@/%@)",dic[@"productCount"],dic[@"originalCount"]];
    if ([dic[@"fundProductStatus"] isEqualToString:@"NORMAL"])
    {
        if ([dic[@"operatorCode"] isEqualToString:@"10086"])
        {
            cell.ui_goodsName.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_traffic.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_unit.textColor = [ColorManager colorWithHexString:@"#59A46C"];
            cell.ui_imageView.image = [UIImage imageNamed:@"geren_bg_yidong"];
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国移动"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
        else if ([dic[@"operatorCode"] isEqualToString:@"10010"])
        {
            cell.ui_goodsName.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_traffic.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_unit.textColor = [ColorManager colorWithHexString:@"#E45252"];
            cell.ui_imageView.image = [UIImage imageNamed:@"geren_bg_liantong"];
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国联通"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
        else if ([dic[@"operatorCode"] isEqualToString:@"10000"])
        {
            cell.ui_goodsName.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_traffic.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_unit.textColor = [ColorManager colorWithHexString:@"#00AED2"];
            cell.ui_imageView.image = [UIImage imageNamed:@"geren_bg_dianxin"];
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国电信"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
    }
    else
    {
        cell.ui_imageView.image = [UIImage imageNamed:@"geren_bg_bukeyong"];
        cell.ui_goodsName.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
        cell.ui_traffic.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
        cell.ui_unit.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.5];
        if ([dic[@"operatorCode"] isEqualToString:@"10086"])
        {
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国移动"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
        else if ([dic[@"operatorCode"] isEqualToString:@"10010"])
        {
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国联通"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
        else if ([dic[@"operatorCode"] isEqualToString:@"10000"])
        {
            cell.ui_goodsName.text = [NSString stringWithFormat:@"流量商品 - 中国电信"];
            NSInteger value = [dic[@"productValue"] integerValue];
            if (value>=1000)
            {
                value = value/1000;
                cell.ui_unit.text = [NSString stringWithFormat:@"G"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%ld",value];
            }
            else if (value<1000)
            {
                cell.ui_unit.text = [NSString stringWithFormat:@"M"];
                cell.ui_traffic.text = [NSString stringWithFormat:@"%@",dic[@"productValue"]];
            }
        }
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary * dic = self.m_traffic[indexPath.row];
    if ([dic[@"fundProductStatus"] isEqualToString:@"NORMAL"])
    {
        XHUseTrafficViewController * viewController = [[XHUseTrafficViewController alloc]init];
        viewController.title = @"使用理财流量";
        viewController.trafficDic = dic;
        viewController.totalRemainCount = self.totalRemainCount;
        [self.navigationController pushViewController:viewController animated:YES];
    }
    else
    {
        [PromptAction showInfoPrompt:@"不可用"];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=108;
    return height;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
